package com.itheima;

import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class FileExplorerGUI extends JFrame {
    //几个全局变量
    private JTextField textField_path;
    private JTextField textField_DocumentName;
    private JTextArea textArea;
    private String CopyPath;
    private String DocumentName;
    public FileExplorerGUI() {
        //标题
        setTitle("简易文件资源管理器");
        //使窗口一直在应用上层
        setAlwaysOnTop(true);
        //居中
        setLocationRelativeTo(null);
        //设置大小
        setPreferredSize(new Dimension(700,500));
        //关闭
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //显示可见0
        setVisible(true);
        //使窗口中的部件大小也可以自适应变化
        pack();
        //调用下面的设置
        configure();
    }
    public void configure() {
        //main为最主要的板子
        JPanel main = new JPanel(new BorderLayout());
        //顶部面板，放路径和文件名的标签和文本框
        JPanel topPanel=new JPanel();
        topPanel.setLayout(new GridLayout(2,2));
        //设置查找前的提示：路径+文件名
        JLabel lable_path=new JLabel("文件路径");
        topPanel.add(lable_path,BorderLayout.NORTH);
        textField_path=new JTextField();
        topPanel.add(textField_path,BorderLayout.NORTH);

        JLabel lable_DocumentName=new JLabel("文件名");
        topPanel.add(lable_DocumentName,BorderLayout.CENTER);
        textField_DocumentName=new JTextField();
        topPanel.add(textField_DocumentName,BorderLayout.CENTER);


        //中间插入一个大文本框，用来显示
        textArea=new JTextArea();
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        //给它加入一个滚动条
        JScrollPane scrollPane=new JScrollPane(textArea);

        //底部面板，放按钮
        JPanel bottomPanel=new JPanel();
        bottomPanel.setLayout(new GridLayout(1,5));
        //设置按钮：搜索，复制，粘贴，重命名，预览，统计
        JButton button_search=new JButton("搜索");
        JButton button_copy=new JButton("复制");
        JButton button_paste=new JButton("粘贴");
        JButton button_rename=new JButton("重命名");
        JButton button_preview=new JButton("预览");
        JButton button_count=new JButton("统计");
        //将上面的按钮添加到窗口
        bottomPanel.add(button_search,BorderLayout.SOUTH);
        bottomPanel.add(button_copy,BorderLayout.SOUTH);
        bottomPanel.add(button_paste,BorderLayout.SOUTH);
        bottomPanel.add(button_rename,BorderLayout.SOUTH);
        bottomPanel.add(button_preview,BorderLayout.SOUTH);
        bottomPanel.add(button_count,BorderLayout.SOUTH);

        //为每个按钮添加行为
        button_search.addActionListener(e -> searchFiles());
        button_copy.addActionListener( e->copyFiles());
        button_paste.addActionListener( e->pasteFiles());
        button_rename.addActionListener( e->renameFiles());
        button_preview.addActionListener( e-> previewFiles());
        button_count.addActionListener( e->countFiles());

        //把三个局部面板放在主板里
        main.add(topPanel,BorderLayout.NORTH);
        main.add(scrollPane,BorderLayout.CENTER);
        main.add(bottomPanel,BorderLayout.SOUTH);

        //把主板加到窗口里
        add(main);

    }
    //搜索函数
    public void searchFiles() {
        //得到目录里的内容
        String catalogue=textField_path.getText();
        //得到文件名的内容
        String DocumentName=textField_DocumentName.getText();
        List<String> result=FileSearch.SearchFiles(catalogue,DocumentName);
        textArea.setText(String.join("\n",result));
    }
    //复制函数
    public void copyFiles() {
        //得到目录里的内容
        String catalogue=textField_path.getText();
        //得到文件名的内容
        DocumentName=textField_DocumentName.getText();
        List<String> result=FileSearch.SearchFiles(catalogue,DocumentName);
        if(!result.isEmpty())
        {
            //里面第一个是我要复制的
            CopyPath=result.get(0);
            //把CopyPath存在一个字符串里
            StringSelection stringSelection=new StringSelection(CopyPath);
            //系统剪贴板
            Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection,null);
            JOptionPane.showMessageDialog(null,"复制成功");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"未找到文件");
        }
    }
    //粘贴函数
    public void pasteFiles() {
        if(CopyPath==null||CopyPath.isEmpty())
        {
            JOptionPane.showMessageDialog(null,"尚未复制文件");
            return;
        }
        //输入要粘贴在的文件路径
        String destination=JOptionPane.showInputDialog("输入目标路径")+DocumentName;

        if(destination!=null&&!destination.isEmpty())
        {
            try {
                FilePaste.pasteFile(CopyPath,destination);
                JOptionPane.showMessageDialog(null, "粘贴完成");
            }
            catch (IOException e)
            {

                JOptionPane.showMessageDialog(null, "粘贴失败");
            }
        }
    }
    //重命名函数
    public void renameFiles() {
        String catalogue=textField_path.getText();
        String DocumentName=textField_DocumentName.getText();
        List<String> result=FileSearch.SearchFiles(catalogue,DocumentName);
        if(!result.isEmpty())
        {
            String oldName=result.get(0);
            String newName=JOptionPane.showInputDialog(null,"请输入文件名");
            if(newName!=null&&!newName.isEmpty())
            {
                Rename.renameFiles(oldName,newName);

                searchFiles();
            }
        }
        else {
            JOptionPane.showMessageDialog(null,"未找到相应文件");
        }
    }
    //预览函数
    public void previewFiles() {
        String filePath=textField_DocumentName.getText();
        String catalogue=textField_path.getText();
        Preview p=new Preview();
        if(p.isImageFile(filePath))
        {
            try
            {
                p.previewImage(catalogue+"/"+filePath);
            }
            catch (IOException e)
            {
                JOptionPane.showMessageDialog(null,"图片预览失败"+e.getMessage());
            }
        }
        else if(filePath.toLowerCase().endsWith(".pdf"))
        {
            p.previewPDF(catalogue+"/"+filePath);
        }
        else if(filePath.toLowerCase().endsWith(".docx")||filePath.toLowerCase().endsWith(".doc"))
        {
            p.previewWord(catalogue+"/"+filePath);
        }
        else if(filePath.toLowerCase().endsWith(".xlsx")||filePath.toLowerCase().endsWith(".xls"))
        {
            p.previewExcel(catalogue+"/"+filePath);
        }
        else
        {
            JOptionPane.showMessageDialog(null,"不支持该类型的文件");
        }


    }
    //统计函数
    public void countFiles(){
        String catalogue=textField_path.getText();
        int num=CountFiles.countFiles(catalogue);
        JOptionPane.showMessageDialog(null,"文件数量"+num);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->{
            FileExplorerGUI frame = new FileExplorerGUI();
            frame.setVisible(true);
        });
    }

}